<?php
	
	abstract class AbstractFieldsetsController extends EavAppController
	{
		/**
		 * Alias of fieldset model
		 * @var string
		 */
		protected $fieldsetModelAlias;
		/**
		 * Display name for fieldset model
		 * @var unknown_type
		 */
		protected $displayName;
		/**
		 * Allow eav path to be added in view path
		 * @var unknown_type
		 */
		public $abstractView = "eav.fieldsets";
		public $helpers	= array('Form','Index','Html');
		function beforeFilter()
		{
			//set fieldmodelalias
			$this->fieldsetModelAlias = $this->modelNames[0];
			$this->displayName = $this->fieldsetModelAlias;
			parent::beforeFilter();
		}
		
		/**
		 * Return instance of fieldsetModel.
		 */
		protected function getFieldsetModel()
		{
			return $this->{$this->fieldsetModelAlias};
		}
		
		function admin_add()
		{
			$this->pageTitle = sprintf(__('Add New %s'),$this->displayName);
			if($this->data)
			{
				if($this->save($this->data))
				{
					$this->successMsg(sprintf(__("%s has been saved"),$this->displayName));
					$this->_afterSave($this->getFieldsetModel()->id);
					$this->redirect(array('action'=>'index'));
				}
			}	
		}
		function admin_index()
		{
			$this->pageTitle = __($this->displayName);
			$this->paginate['limit'] = 10;
			$this->paginate['order'] = array('created DESC');
			$this->set('fieldsets',$this->paginate());	
		}
		
		function admin_edit($id = null)
		{
			$fieldsetData = $this->getFieldsetModel()->findById($id);
			$this->pageTitle = sprintf(__('Edit %s'),$fieldsetData[$this->fieldsetModelAlias]['name'].' '.$this->displayName);
			
			if(!$id || $this->getFieldsetModel()->hasAny(array('id'=>$id)) == false)
			{
				$this->errorMsg(__("Invalid URL."));				
			}
			if($this->data)
			{
				if($this->save($this->data,$id))
				{
					$this->successMsg(sprintf(__("%s has been saved"),$this->displayName));
					$this->_afterSave($this->getFieldsetModel()->id);
					$this->redirect(array('action'=>"index"));
				}
			}else
			{
				$this->getFieldsetModel()->recursive = -1;
				$this->data = $fieldsetData;
			}
			$this->render('admin_add');
		}
		/**
		 * Controller callaback for afterSave
		 */
		function _afterSave()
		{
			
		}
		
		/**
		 * Common method to save fieldset information.
		 * @param $data
		 * @param $id
		 */
		protected function save($data,$id = null)
		{
			$return  = false;
			if($id != null)
			{
				$data[$this->fieldsetModelAlias][$this->getFieldsetModel()->primaryKey] = $id;
			}
			$this->getFieldsetModel()->create();
			if($this->getFieldsetModel()->save($data))
			{
				$return = true;	
			}
			return $return;
		}
		
		function beforeRender()
		{
			$this->set(array('fieldsetModelAlias'=>$this->fieldsetModelAlias,'displayName'=>$this->displayName));
			parent::beforeRender();
		}
		
		function admin_delete($id = null)
		{
			if($id == null)
			{
				$this->errorMsg(__("Invalid URL"));
				$this->redirect($this->referer());
			}
			if($this->getFieldsetModel()->del($id,true))
			{
				$this->successMsg(sprintf(__('%s has been deleted'),$this->displayName));
			}
			$this->redirect($this->referer());
		}
		
	}
?>